﻿//-----------------------------------------------------------------------------------------------------------------
//  Version | DateTime(Create Or Update)  |  UserName		              |  Description						                  |
//-----------------------------------------------------------------------------------------------------------------
//  1.0		  | 2025/08/01 15:39:37		      | WEI.ZHOU (Willis)	        |  Create INIHelper.cs Class
//-----------------------------------------------------------------------------------------------------------------

using System.Runtime.InteropServices;
using System.Text;

namespace Common.Framework.Launcher.Utils
{
  /// <summary>
  /// INI 文件读写类
  /// </summary>
  public class INIHelper
  {

    /// <summary>
    /// INI 文件路径
    /// </summary>
    private readonly string filePath;

    /// <summary>
    /// 构造函数，传入 INI 文件路径
    /// </summary>
    /// <param name="path"></param>
    public INIHelper(string path)
    {
      filePath = path;
    }

    /// <summary>
    /// 导入 Windows API 函数
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <param name="filePath"></param>
    /// <returns></returns>
    [DllImport("kernel32", CharSet = CharSet.Unicode)]
    private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);

    /// <summary>
    /// 导入 Windows API 函数
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="defaultValue"></param>
    /// <param name="retVal"></param>
    /// <param name="size"></param>
    /// <param name="filePath"></param>
    /// <returns></returns>
    [DllImport("kernel32", CharSet = CharSet.Unicode)]
    private static extern int GetPrivateProfileString(string section, string key, string defaultValue,
      StringBuilder retVal, int size, string filePath);

    /// <summary>
    /// 写入 INI 文件
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="value"></param>
    private void Write(string section, string key, string value)
    {
      WritePrivateProfileString(section, key, value, filePath);
    }

    /// <summary>
    /// 读取 INI 文件
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public string Read(string section, string key, string defaultValue = "")
    {
      StringBuilder retVal = new StringBuilder(255);
      GetPrivateProfileString(section, key, defaultValue, retVal, 255, filePath);
      return retVal.ToString();
    }

    /// <summary>
    /// 删除指定键
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    public void DeleteKey(string section, string key)
    {
      Write(section, key, null);
    }

    /// <summary>
    /// 删除指定节
    /// </summary>
    /// <param name="section"></param>
    public void DeleteSection(string section)
    {
      Write(section, null, null);
    }

    /// <summary>
    /// 检查键是否存在
    /// </summary>
    /// <param name="section"></param>
    /// <param name="key"></param>
    /// <returns></returns>
    public bool KeyExists(string section, string key)
    {
      return !string.IsNullOrEmpty(Read(section, key));
    }
  }

}